<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\MsgSet;
use app\api\model\dao\Note;
use app\api\model\dao\NoteReply;
use app\api\model\dao\NoteReplyPraise;
use app\api\model\dao\NoteReplyReply;
use app\api\model\dao\UserAccount;
use app\api\model\data\note\NoteData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetNoteReplyInfo extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteReplyReply;
    private $objNoteReplyPraise;
    private $objUserAccount;
    private $objNoteData;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objUserAccount = new UserAccount();
        $this->objNoteReplyReply = new NoteReplyReply();
        $this->objNoteReplyPraise = new NoteReplyPraise();
        $this->objNoteData = new NoteData();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        if (empty($param['reply_id'])) {
            return $this->getRes(Error::ERR_PARAM);
        }

        $noteReplay = $this->getNoteReplayFromDB($param['reply_id']);
        if (empty($noteReplay)) {
            return $this->getRes();
        }

        $replyToreply = [];
        $userid = [$noteReplay['userid'], $noteReplay['note_userid']];
        $user_account = [];
        $praise_cnt = 0;

        $conds = [
            'note_id' => ['=', $noteReplay['note_id']],
            'reply_id' => ['=', $param['reply_id']],
        ];
        $replyreply_ret = $this->objNoteReplyReply->getList($conds);
        if (!empty($replyreply_ret)) {
            foreach ($replyreply_ret as $val) {
                $replyToreply[$val['reply_id']][] = $val;
                $userid[] = $val['userid'];
                $userid[] = $val['recv_userid'];
            }
        }

        if (!empty($replyToreply[$param['reply_id']])) {
            $replyToreply = $replyToreply[$param['reply_id']];
        }

        $reply_praise_ret = $this->objNoteReplyPraise->getNoteReplyPraiseCount($noteReplay['note_id'], [$param['reply_id']]);
        if (!empty($reply_praise_ret)) {
            foreach ($reply_praise_ret as $val) {
                $replyid2Praise[$val['reply_id']] = $val['cnt'];
            }
        }

        if (!empty($reply_praise_ret[$param['reply_id']])) {
            $praise_cnt = $reply_praise_ret[$param['reply_id']];
        }

        if (!empty($userid)) {
            $userid = array_unique($userid);
            $user_ret = $this->objUserAccount->getUserAccountByUserids($userid);
            if (!empty($user_ret)) {
                $user_ret = $this->modelToArray($user_ret);
                foreach ($user_ret as $val) {
                    $user_account[$val['id']] = $val;
                }
            }
        }

        // 标记已读
        $this->objNoteReplyReply->read($param['reply_id'], $this->user_id);

        // 获取消息通知状态
        $is_open = false;
        $objMsgSet = new MsgSet();
        $msg_set = $objMsgSet->getByUserid($this->user_id);
        if (!empty($msg_set)) {
            if (!empty($msg_set['open']) && !empty($msg_set['note_reply_reply'])) {
                $is_open = true;
            }
        }

        $this->data['list'] = $this->composeData($noteReplay, $user_account, $replyToreply, $praise_cnt, $is_open);
        return $this->getRes();
    }

    /**
     * 从数据库中获取帖子回复列表
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getNoteReplayFromDB($replyid)
    {
        $arrCond = [
            'id' => ['=', $replyid],
            'is_delete' => ['=', Flag::CONST_NOTEREPLAY_DELETE_NOT]
        ];
        $ret = $this->objNoteReply->getNoteReply($arrCond, [], 0, 1, ' create_time desc ');
        if (false === $ret) {
            return [];
        }
        if (empty($ret)) {
            return [];
        }
        return $ret[0];
    }

    public function composeData($note_replay, $user_account, $replyToreply, $praise_count = 0, $is_open)
    {
        $data = [];
        $nickname = $user_account[$note_replay['userid']] ? $user_account[$note_replay['userid']]['nick_name'] : '';
        $photo = $user_account[$note_replay['userid']] ? $user_account[$note_replay['userid']]['avatar'] : '';
        $reply = [];
        $replyreply = !empty($replyToreply) ? $replyToreply : [];
        foreach ($replyreply as $val1) {
            if (empty($user_account[$val1['userid']]) || empty($user_account[$val1['recv_userid']])) {
                continue;
            }
            $reply[] = [
                'send' => [
                    'userid' => $val1['userid'],
                    'isowner' => $val1['note_userid'] == $val1['userid'] ? 1 : 0,
                    'nickname' => $user_account[$val1['userid']]['nick_name'],
                ],
                'recv' => [
                    'userid' => $val1['recv_userid'],
                    'isowner' => $val1['note_userid'] == $val1['recv_userid'] ? 1 : 0,
                    'nickname' => $user_account[$val1['recv_userid']]['nick_name'],
                ],
                'content' => $val1['content']
            ];
        }

        $is_praise = 0;
        $conds = [];
        $conds['note_id'] = ['=', $note_replay['note_id']];
        $conds['reply_id'] = ['=', $note_replay['id']];
        $conds['userid'] = !empty($this->user_id) ? ['=', $this->user_id] : ['=', 0];
        $retCnt = $this->objNoteReplyPraise->getNoteReplyPraiseCountByConds($conds);
        if (!empty($retCnt)) {
            $is_praise = 1;
        }

        $data[] = [
            'note_id' => $note_replay['note_id'],
            'reply_id' => $note_replay['id'],
            'userid' => $note_replay['userid'],
            'nickname' => $nickname,
            'photo' => $photo,
            'content' => $note_replay['content'],
            'time' => $this->objNoteData->caleDate($note_replay['create_time']),
            'praise_count' => $praise_count,
            'msg_set_open' => $is_open ? 1 : 0,
            'reply' => $reply,
            'is_praise' => $is_praise,
        ];
        return $data;
    }
}
